﻿using System;
using System.Data;
using System.Collections.Generic;
//using LTP.Common;
using BOMS.Model;
namespace BOMS.BLL
{
    /// <summary>
    /// 业务逻辑类S_BatSnapshot 的摘要说明。
    /// </summary>
    public class S_BatSnapshot
    {
        const int GENERATION = 2;
        private readonly BOMS.DAL.S_BatSnapshot dal = new BOMS.DAL.S_BatSnapshot();
        public S_BatSnapshot()
        { }
        #region  成员方法
       
       

        ///// <summary>
        ///// 得到一个对象实体，从缓存中。
        ///// </summary>
        //public BOMS.Model.S_BatSnapshot GetModelByCache(int Generation, string EquipID, string BatteryGroup, int BatteryID)
        //{

        //    string CacheKey = "S_BatSnapshotModel-" + Generation + EquipID + BatteryGroup + BatteryID;
        //    object objModel = LTP.Common.DataCache.GetCache(CacheKey);
        //    if (objModel == null)
        //    {
        //        try
        //        {
        //            objModel = dal.GetModel(Generation, EquipID, BatteryGroup, BatteryID);
        //            if (objModel != null)
        //            {
        //                int ModelCache = LTP.Common.ConfigHelper.GetConfigInt("ModelCache");
        //                LTP.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
        //            }
        //        }
        //        catch { }
        //    }
        //    return (BOMS.Model.S_BatSnapshot)objModel;
        //}

        /// <summary>
        /// 取所有的现场主机（设备ID）
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> GetEquipList()
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            DataTable BatSnap = GetList("Generation=" + GENERATION).Tables[0];
            DataTable dtEquip = BatSnap.DefaultView.ToTable(true,"EquipID","HostName");
            for (int i = 0; i < dtEquip.Rows.Count; i++)
            {
                dict.Add(dtEquip.Rows[i]["EquipID"].ToString(), dtEquip.Rows[i]["HostName"].ToString());
            }
            return dict;
        }
        /// <summary>
        /// 取某个设备下的，所有电池组编号
        /// </summary>
        /// <param name="EquipID">设备ID</param>
        /// <returns></returns>
        public List<string> GetBatgrpList(string EquipID)
        {
            List<string> list = new List<string>();
            DataTable BatSnap = GetList("Generation="+GENERATION).Tables[0];
            DataView dv = BatSnap.DefaultView;
            dv.RowFilter = string.Format("EquipID='{0}'",EquipID);
            DataTable dtBatgrp= dv.ToTable(true, "BatteryGroup");
            for (int i = 0; i < dtBatgrp.Rows.Count; i++)
            {
                list.Add(dtBatgrp.Rows[i]["BatteryGroup"].ToString());
            }
            return list;
        }
        /// <summary>
        /// 取某个设备下的，所有电池组编号
        /// </summary>
        /// <param name="EquipID">设备ID</param>
        /// <returns></returns>
        public List<string> GetBatteryList(string EquipID,string BatteryGroup)
        {
            List<string> list = new List<string>();
            DataTable BatSnap = GetList("Generation=" + GENERATION).Tables[0];
            DataView dv = BatSnap.DefaultView;
            dv.RowFilter = string.Format("EquipID='{0}' and BatteryGroup='{1}'", EquipID, BatteryGroup);
            DataTable dtBatgrp = dv.ToTable(true, "BatteryID");
            for (int i = 0; i < dtBatgrp.Rows.Count; i++)
            {
                list.Add(dtBatgrp.Rows[i]["BatteryID"].ToString());
            }
            return list;
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
       
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<BOMS.Model.S_BatSnapshot> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<BOMS.Model.S_BatSnapshot> DataTableToList(DataTable dt)
        {
            List<BOMS.Model.S_BatSnapshot> modelList = new List<BOMS.Model.S_BatSnapshot>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                BOMS.Model.S_BatSnapshot model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new BOMS.Model.S_BatSnapshot();
                    if (dt.Rows[n]["Generation"].ToString() != "")
                    {
                        model.Generation = int.Parse(dt.Rows[n]["Generation"].ToString());
                    }
                    model.EquipID = dt.Rows[n]["EquipID"].ToString();
                    model.BatteryGroup = dt.Rows[n]["BatteryGroup"].ToString();
                    if (dt.Rows[n]["BatteryID"].ToString() != "")
                    {
                        model.BatteryID = int.Parse(dt.Rows[n]["BatteryID"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  成员方法
    }
}

